//==================================================================
/// MemUtils.h
///
/// Created by Davide Pasca - 2009/10/8
/// See the file "license.txt" that comes with this project for
/// copyright info. 
//==================================================================

#ifndef MEMUTILS_H
#define MEMUTILS_H

#include "DContainers.h"

//==================================================================
template <class _T>
class adel
{
	_T	*mPtr;

public:
	adel( _T *ptr=0 )
	{
		mPtr = ptr;
	}

	adel &operator=( _T *ptr )
	{
		if ( mPtr && mPtr != ptr )
			DSAFE_DELETE( mPtr );

		mPtr = ptr;

		return *this;
	}

	~adel()
	{
		if ( mPtr )
			delete mPtr;
	}

	const _T *operator->() const	{ return mPtr; }
		  _T *operator->()			{ return mPtr; }

	const _T &operator*() const		{ return *mPtr; }
		  _T &operator*()			{ return *mPtr; }

	const _T *get() const			{ return mPtr;	}
		  _T *get()					{ return mPtr;	}

		  _T *release()				{ _T *pRet = mPtr; mPtr = NULL; return pRet; }
};

//==================================================================
template <class _T>
class VecOwn : public DVec<_T>
{
public:
	~VecOwn()
	{
		for (size_t i=0; i < size(); ++i)
		{
			DSAFE_DELETE( (*this)[i] );
		}
	}

	void clear_free()
	{
		for (size_t i=0; i < size(); ++i)
		{
			DSAFE_DELETE( (*this)[i] );
		}

		clear();
	}
};

//==================================================================


#endif
